package com.ant.time.line.views;

import android.accessibilityservice.AccessibilityService;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathDashPathEffect;
import android.graphics.PathEffect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;

import com.ant.time.line.R;
import com.ant.time.line.utils.AntLog;
import com.ant.time.line.utils.DeviceUtils;

/**
 * Created with AS
 * User: Ant
 * Date: 2018/10/7
 * Time: 11:41
 * Name: TimeLineView
 * Description:
 *
 * @author Ant
 * @version 1.0.0
 */
public class TimeLineView extends android.support.v7.widget.AppCompatTextView {

    public static final int TYPE_DEFAULT = 0;
    public static final int TYPE_TOP = 1;
    public static final int TYPE_BOTTOM = 2;
    public static final int TYPE_ALL = 7;

    public static final int TYPE_DASH = 3;
    public static final int TYPE_DASH_TOP = 4;
    public static final int TYPE_DASH_BOTTOM = 5;
    public static final int TYPE_DASH_ALL = 8;

    public static final int TYPE_RANGE = 6;


    private int type = TYPE_DEFAULT;


    private int lineColor, rangColor, dashLineColor;
    private float lineWidth = 0, radius = 0, rangWidth = 0;

    private Paint linePaint, dashLinePaint, dashPaint, rangPaint, tinPaint;

    private Path path = new Path();
    private String textTag = "停";


    public TimeLineView(Context context) {
        this(context, null);
    }

    public TimeLineView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TimeLineView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setLayerType(LAYER_TYPE_SOFTWARE, null);

        if (attrs == null)
            return;

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TimeLineView);
        lineColor = typedArray.getColor(R.styleable.TimeLineView_timeLineColor, Color.GREEN);
        dashLineColor = typedArray.getColor(R.styleable.TimeLineView_timeDashLineColor, Color.RED);
        rangColor = typedArray.getColor(R.styleable.TimeLineView_timeRangColor, Color.YELLOW);
        lineWidth = typedArray.getDimension(R.styleable.TimeLineView_lineWidth, getdp2px(3));
        radius = typedArray.getDimension(R.styleable.TimeLineView_lineRadius, getdp2px(5l));
        rangWidth = typedArray.getDimension(R.styleable.TimeLineView_timeRangWidth, getdp2px(20));
        typedArray.recycle();

        // init paint and value
        linePaint = getMyPaint(lineColor);
        dashPaint = getMyPaint(dashLineColor);
        dashPaint.setStyle(Paint.Style.STROKE);
        dashLinePaint = getMyPaint(dashLineColor);
        dashLinePaint.setPathEffect(new DashPathEffect(new float[]{20, 20}, 0));
        // tagText paint
        tinPaint = getMyPaint(Color.WHITE);
        tinPaint.setTextSize(rangWidth * 0.8f);
        // rang paint
        rangPaint = getMyPaint(rangColor);
        rangPaint.setStrokeCap(Paint.Cap.ROUND);
        rangPaint.setStrokeWidth(rangWidth);

    }

    private Paint getMyPaint(int color) {
        Paint paint = new Paint();
        paint.setStrokeWidth(lineWidth);
        paint.setColor(color);
        paint.setAntiAlias(true);
        return paint;
    }

    private float getdp2px(float dp) {

        return DeviceUtils.dipToPx(getContext(), dp);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

    }

    public void setType(int type) {
        this.type = type;
        // 控制开始文字显示和结尾的文字显示 利用布局的gravity
        if (type == TYPE_DASH_TOP || type == TYPE_BOTTOM) {
            setGravity(Gravity.CENTER_VERTICAL);
        } else {
            setGravity(Gravity.TOP);
        }

        invalidate();
    }

    public void setTextTag(String textTag) {
        this.textTag = textTag;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int x = getRight() - getPaddingRight();
        float r = radius + lineWidth / 2;
        switch (type) {
            case TYPE_TOP:
                linePaint.setStyle(Paint.Style.STROKE);
                canvas.drawCircle(x, r, radius, linePaint);
                linePaint.setStyle(Paint.Style.FILL);
                canvas.drawLine(x, r + r, x, getBottom(), linePaint);
                break;
            case TYPE_BOTTOM:
                canvas.drawLine(x, 0, x, getBottom() - r - r, linePaint);
                linePaint.setStyle(Paint.Style.STROKE);
                canvas.drawCircle(x, getBottom() - r, radius, linePaint);
                linePaint.setStyle(Paint.Style.FILL);
                break;
            case TYPE_ALL:
                linePaint.setStyle(Paint.Style.STROKE);
                canvas.drawCircle(x, r, radius, linePaint);
                canvas.drawCircle(x, getBottom() - r, radius, linePaint);
                linePaint.setStyle(Paint.Style.FILL);
                canvas.drawLine(x, 2 * r, x, getBottom() - 2 * r, linePaint);
                break;
            case TYPE_DASH:
                canvas.drawLine(x, 0, x, getBottom(), dashLinePaint);
                break;
            case TYPE_DASH_TOP:
                canvas.drawCircle(x, r, radius, dashPaint);
                canvas.drawLine(x, r + r, x, getBottom(), dashLinePaint);
                break;
            case TYPE_DASH_BOTTOM:
                canvas.drawLine(x, 0, x, getBottom() - 2 * r, dashLinePaint);
                canvas.drawCircle(x, getBottom() - r, radius, dashPaint);
                break;
            case TYPE_DASH_ALL:
                canvas.drawCircle(x, r, radius, dashPaint);
                canvas.drawCircle(x, getBottom() - r, radius, dashPaint);
                canvas.drawLine(x, 2 * r, x, getBottom() - 2 * r, dashLinePaint);
                break;
            case TYPE_RANGE:
                float space = rangWidth / 2;
                canvas.drawLine(x, 0, x, getBottom(), linePaint);
                canvas.drawLine(x, space, x, getBottom() - space, rangPaint);

                if (null != textTag && textTag.length() > 0) {
                    float size = tinPaint.getTextSize();
                    canvas.drawText(textTag, x - size / 2, space + size, tinPaint);
                }
                break;
            case TYPE_DEFAULT:
            default:
                canvas.drawLine(x, 0, x, getBottom(), linePaint);
        }
    }
}